package cn.yld.car.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yld.car.domain.Car;
import com.yld.car.domain.SearchDetailCar;
import com.yld.car.market.R;

public class CarTypeAdapter extends BaseAdapter {

	private ArrayList<Car> all;

	public CarTypeAdapter(ArrayList<Car> all, Context context) {
		this.all = all;
		this.context = context;
	}

	private Context context;

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return all.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return all.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {

			convertView = LayoutInflater.from(context).inflate(
					R.layout.car_items, null);
		}

		Car c = all.get(position);

		if (c instanceof SearchDetailCar) {

			SearchDetailCar s = (SearchDetailCar) c;

			ImageView image = (ImageView) convertView
					.findViewById(R.id.itemCarImage);
			String type = s.getXhorgh();
			// System.out.println(type);
			if ("现货".equals(type)) {
				image.setImageResource(R.drawable.xianhuo);
			} else {
				image.setImageResource(R.drawable.qihuo);
			}
			TextView typeText = (TextView) convertView
					.findViewById(R.id.itemType);
			typeText.setVisibility(View.VISIBLE);
			typeText.setText(s.getPinpainame() + "-" + s.getXiliename());
			// TextView titleText = (TextView) convertView
			// .findViewById(R.id.itemTitle);
			//
			// titleText.setText(s.getPinpainame());
			TextView abbreviationText = (TextView) convertView
					.findViewById(R.id.itemAbbreviation);

			abbreviationText.setText(s.getName());
			TextView priceText = (TextView) convertView
					.findViewById(R.id.itemPrice);

			priceText.setText(s.getPress() + "万");
			TextView configText = (TextView) convertView
					.findViewById(R.id.itemSelectConfig);

			configText.setText(s.getXppz());
			TextView publishText = (TextView) convertView
					.findViewById(R.id.itemPublishDate);

			String date = s.getAdddate();
			date = date.substring(0, date.lastIndexOf(" "));
			publishText.setText(date);
			TextView dealerText = (TextView) convertView
					.findViewById(R.id.itemDealerName);
			dealerText.setText(s.getCompanyname());
			
		}
		convertView
				.setBackgroundResource(android.R.drawable.list_selector_background);
		return convertView;
	}

}
